package com.inossem.warehousestorage_inossem.activity.in_storage;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.inossem.loadandrefresh.PullToRefreshLayout;
import com.inossem.loadandrefresh.pullableview.PullableListView;
import com.inossem.utils.base.QRCodeUtils;
import com.inossem.utils.base.Utils;
import com.inossem.utils.constant.QRCodeConstant;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.base.BaseActivity;
import com.inossem.warehousestorage_inossem.adapter.inStorage.TenderInStorageListAdapter;
import com.inossem.warehousestorage_inossem.bean.inStorage.InStorageTenderBaseInfoBean;
import com.inossem.warehousestorage_inossem.bean.inStorage.InStorageTenderListBean;
import com.inossem.warehousestorage_inossem.listener.GsonBeanListener;
import com.inossem.warehousestorage_inossem.request.inStorage.InStorageTenderBaseInfoRequest;
import com.inossem.warehousestorage_inossem.request.inStorage.InStorageTenderListRequest;
import com.inossem.warehousestorage_inossem.utils.ActivityUtils;
import com.inossem.warehousestorage_inossem.utils.SPUtils;

import java.util.ArrayList;
import java.util.List;

//免检入库
public class InStorageTenderListActivity extends BaseActivity {

    private TextView title;

    private PullToRefreshLayout refresh;

    private PullableListView tenderInStorageList;

    private List<InStorageTenderListBean.TenderInStorageListBean> allData;

    private TenderInStorageListAdapter adapter;

    private String mContractCode, mPurchase;

    @Override
    protected void onCreateImpl(Bundle savedInstanceState) {
        setContentView(R.layout.activity_in_storage_tender_list);
        initView(InStorageTenderListActivity.this);
        initData(InStorageTenderListActivity.this);
    }

    @Override
    protected void onResumeImpl() {

    }

    private void initView(Activity activity) {
        title = (TextView) activity.findViewById(R.id.headerTitle);
        tenderInStorageList = (PullableListView) activity.findViewById(R.id.tenderInStorageList);
        refresh = (PullToRefreshLayout) activity.findViewById(R.id.tenderInStorageListRefresh);
    }

    private void initData(Activity activity) {
        title.setText(getResources().getString(R.string.activity_in_storage_manager_for_mining_warehousing));
        ActivityUtils.initBackAndFunction(activity, getResources().getString(R.string.activity_in_storage_tender_list_warehousing), new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                finish();
            }
        });

        tenderInStorageList.setPullUpFlag(false);
        tenderInStorageList.setPullDownFlag(false);
        resetData(activity);
        setmScanListener((String scanResult, int length) -> {
            mContractCode = "";
            mPurchase = QRCodeUtils.DecryptQRCode(scanResult);
            getData(activity, mContractCode, mPurchase);
        },  QRCodeConstant.TYPE_PURCHASE_ORDER);
        getBaseInfo(activity);
    }

    private void resetData(Context context) {
        mContractCode = "";
        mPurchase = "";
        allData = new ArrayList<>();
        addInStorageTenderListData(context, allData, tenderInStorageList, true);
    }

    private void addInStorageTenderListData(Context context, List<InStorageTenderListBean.TenderInStorageListBean> tenderInStorageList, ListView listView, boolean flag) {
        if (flag) {
            adapter = new TenderInStorageListAdapter(context, tenderInStorageList, mContractCode, mPurchase, (EditText contractCode, EditText purchase) -> {
                NoDoubleClickListener noDoubleClickListener = new NoDoubleClickListener() {
                    @Override
                    public void onNoDoubleClick(View v) {
                        String checkResult = check(contractCode, purchase);
                        if (checkResult == null) {
                            Utils.closeSoftKeyboard(context, v);
                            mContractCode = contractCode.getText().toString();
                            mPurchase = purchase.getText().toString();
                            getData(context, mContractCode, mPurchase);
                        } else {
                            Utils.middleToast(context, checkResult);
                        }
                    }
                };
                return noDoubleClickListener;
            });
            listView.setAdapter(adapter);
        }
    }

    private String check(EditText contractCode, EditText purchase) {
        String result = null;
        if ("".equals(contractCode.getText().toString()) && "".equals(purchase.getText().toString())) {
            result = getResources().getString(R.string.activity_in_storage_free_list_fill_in_at_least);
        }
        return result;
    }

    private void getData(Context context, String contractCode, String purchaseOrder) {
        InStorageTenderListRequest.request(context, contractCode, purchaseOrder, new GsonBeanListener<InStorageTenderListBean>() {
            @Override
            public void onSuccess(InStorageTenderListBean bean) {
                allData = bean.getBody();
                if (allData == null || allData.isEmpty()) {
                    Utils.middleToast(context, getResources().getString(R.string.activity_out_storage_sale_list_no_data));
                }
                addInStorageTenderListData(context, ActivityUtils.getShowData(allData, 0), tenderInStorageList, true);
            }
        });
    }
    private void getBaseInfo (Context context) {
        InStorageTenderBaseInfoRequest.request(context, new GsonBeanListener<InStorageTenderBaseInfoBean>() {
            @Override
            public void onSuccess(InStorageTenderBaseInfoBean bean) {
                Gson gson = new Gson();
                String json = gson.toJson(bean);
                SPUtils.saveTenderInStorageBaseInfo(context , json);
            }
        });
    }
    public interface InStorageTenderListener {
        NoDoubleClickListener getNoDoubleClickListener(EditText contractCode, EditText purchase);
    }

}
